package com.syh.controller;

import com.syh.mapper.CronMapper;
import com.syh.pojo.Cron;
import com.syh.pojo.Orders;
import com.syh.pojo.People;
import com.syh.pojo.Shop;
import com.syh.service.PeopleService;
import com.syh.service.ShopService;
import com.syh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author syh
 * @date 2023/3/23-17:12
 */
@SuppressWarnings("all")
@RestController
@Api(value ="商品管理",tags = {"商品管理接口"})
@RequestMapping("/shop")
@EnableScheduling
public class ShopController {

    @Autowired
    ShopService shopService;
    @Autowired
    PeopleService peopleService;
    Random r1=new Random();
    @Autowired
    CronMapper cronMapper;

    @PostMapping("/selectallShop")
    @ApiOperation(value = "查询商品")
    public Result selectallShopByPage(){
        //分页查询商品列表
        try {
            List<Shop> shopList = shopService.selectAll();
            return Result.ok(shopList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查詢失敗");
        }
    }
    @PostMapping("/shopgoods")
    @ApiOperation(value = "购买商品")
    public  Result shopgoods(@RequestBody Orders orders){
        try {
            Shop shop=shopService.selectByID(orders.getId());
            shop.setGoodsNumber(orders.getOrderNumber());
            String name=orders.getUserName();
            shopService.shopping(shop,name);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("购买失敗");
        }
    }
    @PostMapping("/randomshopgoods")
    @ApiOperation(value = "随机购买商品")
    public  Result randomshopgoods(){
        try {
            List<Shop> shopList = shopService.selectAll();
            List<People> people = peopleService.selectAll();
            Random r = new Random();
            int Arraylength1= r.nextInt(shopList.size());
            int Arraylength2=r.nextInt(people.size());

            Shop shop=shopList.get(Arraylength1);
            int shopNumber= r.nextInt(shop.getGoodsNumber())+1;
            shop.setGoodsNumber(shopNumber);
            People people1=people.get(Arraylength2);
            shopService.shopping(shop,people1.getName());
            Cron cron=new Cron();
            cron.setCronId("1");
            int time=r.nextInt(100)+10;
            String cronTime="0/"+time+" * * * * ?";
            cron.setCron(cronTime);
            cronMapper.updateByPrimaryKey(cron);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("购买失敗");
        }
    }
    @GetMapping("/insertShop")
    @ApiOperation(value = "新增商品")
    public  Result insertShop(Shop shopList){
        try {
            shopService.insertshop(shopList);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("新增失敗");
        }
    }
    @DeleteMapping("/deleteShop")
    @ApiOperation(value = "删除商品")
    public  Result deleteShop(@RequestBody Shop shopList){
        try {
            System.out.println(shopList.getId());
            shopService.deleteshop(shopList);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除失敗");
        }
    }
    @PostMapping("/addgoods")
    @ApiOperation(value = "补充货物")
    public Result addgoods(){
        //分页查询商品列表
        try {
           shopService.bh();
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查詢失敗");
        }
    }
    @PostMapping("/modifyprice")
    @ApiOperation(value = "修改单价")
    public Result modifyprice(){
        //分页查询商品列表
        try {
            List<Shop> shopList=shopService.selectAll();
            for (Shop s : shopList) {
                shopService.updateshopbyprices(s);
            }
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查詢失敗");
        }
    }
//    @GetMapping("/selectname")
//    @ApiOperation(value = "根据姓名查操作日志")
//    public Result selectEmploylog(@RequestBody String name){
//        //分页查询商品列表
//        try {
//            List<EmployeeFh> i = employeeService.selectname(name);
//            return Result.ok(i);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.error("查詢失敗");
//        }
//    }


}
